*************************************************************************************************
*This file builds exposure to robots measure by period. Adapted version of Pascual Restrepo, JPE*
*************************************************************************************************

**********************************************
**Step 1: main IV variable used in the paper**
**********************************************
use "$list/czones_list.dta", clear
save "$clean_data_automation/czones_exposure_robots.dta", replace

**measure for all industries**
use "$clean_data_lmarket/czone_1970_emp_by_ifr19.dta", clear
merge m:1 industry_ifr19 using "$clean_data_automation/apr_measures_ifr19.dta", assert(3) nogenerate
drop *_us_* /*Remove endogenous one*/
collapse (mean) apr_* [w=emppriv], by(czone) fast
rename apr_* expof_*
merge 1:1 czone using "$clean_data_automation/czones_exposure_robots.dta", assert(3) nogenerate 
save "$clean_data_automation/czones_exposure_robots.dta", replace

**measure excluding one industry at the time**
foreach ind in agr aut con ele foo fur man mba mma mpr mnr mng pap pet res ser tex uti veh {
use "$clean_data_lmarket/czone_1970_emp_by_ifr19.dta", clear
merge m:1 industry_ifr19 using "$clean_data_automation/apr_measures_ifr19.dta", assert(3) nogenerate
drop *_us_* /*Remove endogenous one*/
replace industry_ifr19 = "mbasic" if industry_ifr19=="metal_basic"
replace industry_ifr19 = "mmachinery" if industry_ifr19=="metal_machinery"
replace industry_ifr19 = "mproducts" if industry_ifr19=="metal_products"
replace industry_ifr19 = "mnral" if industry_ifr19=="mineral"
replace industry_ifr19 = "mng" if industry_ifr19=="mining"
replace industry_ifr19 = substr(industry_ifr19, 1, 3)
foreach var of varlist apr_*{
replace `var'=0 if industry_ifr=="`ind'"
}
collapse (mean) apr_* [w=emppriv], by(czone) fast
rename apr_* expon_`ind'_*
merge 1:1 czone using "$clean_data_automation/czones_exposure_robots.dta", assert(3) nogenerate 
save "$clean_data_automation/czones_exposure_robots.dta", replace
}

**OLS measure (employment shares in 1990)**
use "$clean_data_lmarket/czone_1990_emp_by_ifr19.dta", clear
merge m:1 industry_ifr19 using "$clean_data_automation/apr_measures_ifr19.dta", assert(3) nogenerate
keep czone emppriv *_us_* /*Keep endogenous one*/
collapse (mean) apr_us* [w=emppriv], by(czone) fast
rename apr_* expof_*
merge 1:1 czone using "$clean_data_automation/czones_exposure_robots.dta", assert(3) nogenerate 
save "$clean_data_automation/czones_exposure_robots.dta", replace

*********************************************************************************
**Step 2: Robustness using 1990 employment shares to measure exposure to robots** 
*********************************************************************************
**measure for all industries**
use "$clean_data_lmarket/czone_1990_emp_by_ifr19.dta", clear
merge m:1 industry_ifr19 using "$clean_data_automation/apr_measures_ifr19.dta", assert(3) nogenerate
keep czone industry_ifr emppriv apr_*_qo* /*Remove endogenous one*/
collapse (mean) apr_* [w=emppriv], by(czone) fast
rename apr_* expof_b90_*
merge 1:1 czone using "$clean_data_automation/czones_exposure_robots.dta", assert(3) nogenerate 
save "$clean_data_automation/czones_exposure_robots.dta", replace

cap drop tempid			
save "$clean_data_automation/czones_exposure_robots.dta", replace
